package wiz.client.model;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;

public class WizModelFactory {
	
	 WizModelAutobeanFactory fFactory = GWT.create(WizModelAutobeanFactory.class);
	
	public WizModelFactory() {
		
	}
	
	public WizPlayer createPlayer() {
		return fFactory.player().as();
	}
	
	public WizGameRound createRound() {
		WizGameRound round = fFactory.round().as();
		round.setRoundScores(new ArrayList<WizPlayerRoundScore>());
		return round;
	}
	
	public WizGameModel createModel() {
		WizGameModel as = fFactory.model().as();
		as.setWizPlayers(new ArrayList<WizPlayer>());
		as.setGameRounds(new ArrayList<WizGameRound>());
		return as;
	}
	
	public WizPlayerRoundScore createPlayerRoundScore() {
		return fFactory.playerRoundScore().as();
	}
	
	public String modelToJSON(WizGameModel model){
		AutoBean<WizGameModel> bean = AutoBeanUtils.getAutoBean(model);
		return AutoBeanCodex.encode(bean).getPayload();
	}
	
	public WizGameModel jsonToModel(String json){
	    AutoBean<WizGameModel> bean = AutoBeanCodex.decode(fFactory, WizGameModel.class, json);
	    return bean.as();
	}
	

}
